/**
 * 项目project.js
 * 版本号： v1.0.0
 */
$(function () {
    var lastChecked = null;
    var url = CONTEXT_PATH + '/projectManagement/projectList.do';
    var prTypeFromJump = $('#prTypeFromJump').val();
    if(prTypeFromJump){
        url = url + '?prTypeFromJump=' +prTypeFromJump;
    }
    $("#project_dataList").datagrid({
        fitColumns: true,
        fit: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: url,
        idField: 'prId',
        frozenColumns: [[
            {field: '', checkbox: true}
        ]],
        columns: [
            [
                {field: 'prName', title: '项目名称', width: 260, fixed: true},
                {field: 'prType',title: '项目类型', width: 70, fixed: true,  formatter:function(val){
                        if(val) {
                            return val.propertyName;
                        }
                }},
                {field: 'prLocal', title: '项目标段', width: 260 },
                {field: 'prLeader', title: '负责人', width: 90, fixed: true},
                {field: 'prRealStart', title: '实际开始日期', width: 100, fixed: true, formatter:function(val){
                        return formatCalendar(val);
                }},
                {field: 'prRealEnd', title: '实际结束日期', width: 100, fixed: true, formatter:function(val){
                        return formatCalendar(val);
                }}
            ]
        ],
        onDblClickRow: function (index, row) {
            project_view(row);
        },
        onCheck:function(rowIndex, rowData){
            var row = $("#project_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#project_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#project_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#project_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });
});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            //加载所属公司下拉列表
                            project_loadCompany();
                            project_findList();
                        }
                    });
                }
                //查看按钮
                if("checkBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: project_view
                    });
                }
                //智慧工地按钮
                if("wisBtn"  == btnOptions[i].key){
                    toolbar.push({
                        width:100,
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: windom_project_view
                    });
                }
            }
        }
    }
    return toolbar;
}

//查看项目
function project_view(row){

    if(!row.prId){
        row = $("#project_dataList").datagrid("getSelected");
    }
    var prId = row.prId;
    var width = window.parent.window.document.body.offsetWidth;
    var height = window.parent.window.document.body.offsetHeight;
    var url = CONTEXT_PATH + "/projectManagement/projectView.do?prId=" + prId;
    var option = {
        title : "项目查看",
        iconCls : "icon-custom-project",
        width : width,
        height : height,
        closable : true,
        minimizable : false,
        style : {"display" : "block"},
        top:1,
        left:1,
        closed : false,
        content : "<iframe scrolling = 'yes' frameborder = '0'  src='" + url + "' style='width:100%;height:100%'></iframe>"
    }
    window.parent.openProjectViewWindow(option);
}

//跳转到智慧工地前端页面
function windom_project_view(row){

    if(!row.prId){
        row = $("#project_dataList").datagrid("getSelected");
    }
    var prId = row.prId;
    var width = window.parent.window.document.body.offsetWidth;
    var height = window.parent.window.document.body.offsetHeight;
    var url = "http://smartsitedisplayplatformtr.d.guijianyun.com:8000/?projectId=" + prId;
    var option = {
        title : "智慧工地",
        iconCls : "icon-custom-project",
        width : width,
        height : height,
        closable : true,
        minimizable : false,
        style : {"display" : "block"},
        top:1,
        left:1,
        closed : false,
        content : "<iframe scrolling = 'yes' frameborder = '0'  src='" + url + "' style='width:100%;height:100%'></iframe>"
    }
    window.parent.openProjectViewWindow(option);
}

//查询项目
function project_findList() {
    $("#project_dataList").datagrid("reload",{prName:$("#prName_").val(),
        prType:$("#prType_").combobox("getValue")});
    //清楚选中项
    $("#project_dataList").datagrid("clearSelections");
}

/**
 * 加载所有的公司下拉
 */
function project_loadCompany() {
    //所属公司下拉树
    $('#companyTree').combotree({
        url: CONTEXT_PATH + "/company/getTreeCompany.do",
        onSelect:function(node){
            $('#companyId').val(node.id);
        }
    });

}

/***
 * 动态刷新页面
 * @param prId
 */
function refreshPageNumber(csId) {
    //获取dataGrid的列表对象属性
    var $datagrid = $("#" + csId).datagrid("options");
    if ($datagrid != undefined) {
        $datagrid.pageNumber = 1;
    }
    //获取dataGrid的分页对象
    var $getPager = $("#" + csId).datagrid('getPager');
    var $pagination = $($getPager).pagination("options");
    if ($pagination != undefined) {
        $pagination.pageNumber = 1;
    }
}